package mutation;

import ga.Individual;

import java.util.*;

import model.TestClusterBuilder;

import operations.Operation;
import operations.ValuesGenerator;
import util.*;


public class RandomInsertMethodCall_1 extends MutationStrategy {

	@Override
	public Individual mutate(Individual individual) {

		Individual mutatedIndividual=individual.clone();
		List<Operation> ops =mutatedIndividual.getStatements();
		TestClusterBuilder TestCluster=gaInfo.getTestCluster();
		try
		{	int i=1;
			double p_insert_st=GAConfig.p_statement_insertion;
			p_insert_st=java.lang.Math.pow(p_insert_st, i);
			while(Randomness.random.nextDouble()<p_insert_st){
				int randPos = ValuesGenerator.getRandomIntBetween(0,ops.size()-1);
				List<Operation>  rep= new LinkedList<Operation>();
				rep.addAll(ops.subList(0, randPos));
			// See if individual length is reached GAConfig.TestCaseSize
				int L=GAConfig.TestCaseSize-ops.size();
	    	  rep.addAll(Operation.randomlyGenerateOperation(TestCluster,1));
	    	  	rep.addAll(ops.subList(randPos, ops.size()));
	    	  	ops.clear();
	    	  	ops.addAll(rep);
	    		p_insert_st=java.lang.Math.pow(p_insert_st, ++i);
			}

		} catch (Throwable e){
			System.err.println("Insert Random Method Call Class:" + e.getMessage());
		}	

		if(DebugConfig.InsertMethodValueClass){
			Log.log("--------------------individual1----------------------");
			Log.log(individual.toString()+ " size:"+ individual.getSize());
			Log.log("--------------------mutatedIndividual----------------------");
			Log.log(mutatedIndividual.toString()+ " size:"+ mutatedIndividual.getSize());
		}
		
		return mutatedIndividual;
		
	}
	@Override
	public boolean getMutationFlag(int size) {
		if(ValuesGenerator.getBoolean(1.0f/size))
			return true;
		return false;
	}

}
